package com.engine.cese.duss.srm.offlinewf.web;

import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.general.Util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.Map;

/**
 * @Title: Ecology9
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2021-05-19
 * @Description: 线下招采过程管理
 * @ModifyLog:
 **/

public class WeaOfflineWfAction {
    /**
     * @Description: 获取招标采购立项流程状态
     * @Param: [request, response]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2021-05-19
     */
    @GET
    @Path("/getCglxWfStatus")
    @Produces({MediaType.TEXT_PLAIN})
    public String getCglxWfStatus(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            String cglxd = Util.null2String( request.getParameter( "cglxd" ) );
            RecordSet rs = new RecordSet();
            String sql = "select currentnodetype from workflow_requestbase where requestid=?";
            rs.executeQuery( sql, cglxd );
            rs.next();
            String currentnodetype = Util.null2String( rs.getString( "currentnodetype" ) );
            apidatas.put( "currentnodetype", currentnodetype );
            apidatas.put( "api_status", true );
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put( "api_status", false );
            apidatas.put( "api_errormsg", "catch exception : " + e.getMessage() );
        }
        return JSONObject.toJSONString( apidatas );
    }
    /**
     * @Description: 根据采购立项单获取流程状态
     * @Param: [request, response]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2021/3/1
     */
    @GET
    @Path("/getCglxd2Status")
    @Produces({MediaType.TEXT_PLAIN})
    public String getCglxd2Status(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            String cglxd = Util.null2String( request.getParameter( "cglxd" ) );
            String table = Util.null2String( request.getParameter( "table" ) );
            JSONObject datas = new JSONObject();
            RecordSet rs = new RecordSet();
            BaseBean baseBean = new BaseBean();
            String formTable = baseBean.getPropValue( "SrmWfTab", table );
            String sql = "select t1.requestid,t2.currentnodetype from " + formTable
                    + " t1 left join workflow_requestbase t2 on t1.requestid=t2.requestid where t1.cglxd = ?";
            rs.executeQuery( sql,cglxd);
            rs.next();
            String requestid = Util.null2String( rs.getString( "requestid" ) );
            String currentnodetype = Util.null2String( rs.getString( "currentnodetype" ) );
            datas.put( "requestid", requestid );
            datas.put( "currentnodetype", currentnodetype );
            apidatas.put( "datas", datas );
            apidatas.put( "api_status", true );
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put( "api_status", false );
            apidatas.put( "api_errormsg", "catch exception : " + e.getMessage() );
        }
        return JSONObject.toJSONString( apidatas );
    }

    /**
     * @Description: 根据采购立项单获取需要购买标书，投标保证金
     * @Param: [request, response]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2021/3/1
     */
    @GET
    @Path("/getBspsData")
    @Produces({MediaType.TEXT_PLAIN})
    public String getBspsData(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            String cglxd = Util.null2String( request.getParameter( "cglxd" ) );
            JSONObject datas = new JSONObject();
            RecordSet rs = new RecordSet();
            BaseBean baseBean = new BaseBean();
            String bspsxx = baseBean.getPropValue( "SrmWfTab", "bspsxx" );
            String sql = "select xygmbs,tbbzj from "+bspsxx+" where cglxd = ?";
            rs.executeQuery( sql,cglxd);
            rs.next();
            String xygmbs = Util.null2String( rs.getString( "xygmbs" ) );
            String tbbzj = Util.null2String( rs.getString( "tbbzj" ) );
            datas.put( "xygmbs", xygmbs );
            datas.put( "tbbzj", tbbzj );
            apidatas.put( "datas", datas );
            apidatas.put( "api_status", true );
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put( "api_status", false );
            apidatas.put( "api_errormsg", "catch exception : " + e.getMessage() );
        }
        return JSONObject.toJSONString( apidatas );
    }
    /**
     * @Description: 根据表名字段名获取workflowid和fieldid
     * @Param: [request, response]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2021/3/1
     */
    @GET
    @Path("/getFieldid")
    @Produces({MediaType.TEXT_PLAIN})
    public String getFieldid(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            BaseBean baseBean = new BaseBean();
            String formtable = Util.null2String( request.getParameter( "formtable" ) );
            String fieldname = Util.null2String( request.getParameter( "fieldname" ) );
            String table = baseBean.getPropValue( "SrmWfTab", formtable );
            RecordSet rs = new RecordSet();
            String sql = "select concat('field',t1.id)as fieldid from workflow_billfield t1 \n" +
                    "left join workflow_base t2 on t1.billid=t2.formid \n" +
                    "where t1.fieldname=? and concat('formtable_main_',abs(t2.formid)) =?";
            rs.executeQuery( sql, fieldname, table);
            rs.next();
            String fieldid = Util.null2String( rs.getString( "fieldid" ) );
            apidatas.put( "fieldid", fieldid );
            apidatas.put( "api_status", true );
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put( "api_status", false );
            apidatas.put( "api_errormsg", "catch exception : " + e.getMessage() );
        }
        return JSONObject.toJSONString( apidatas );
    }
    /**
     * @Description: 根据表名字段名获取workflowid
     * @Param: [request, response]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2021/3/1
     */
    @GET
    @Path("/getWorkflowid")
    @Produces({MediaType.TEXT_PLAIN})
    public String getWorkflowid(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            BaseBean baseBean = new BaseBean();
            String formtable = Util.null2String( request.getParameter( "formtable" ) );
            String table = baseBean.getPropValue( "SrmWfTab", formtable );
            RecordSet rs = new RecordSet();
            String sql = "select t2.id as workflowid from workflow_billfield t1 left join workflow_base t2 on t1.billid=t2.formid \n" +
                    "where concat('formtable_main_',abs(t2.formid)) =? group by t2.id";
            rs.executeQuery( sql,table);
            rs.next();
            String workflowid = Util.null2String( rs.getString( "workflowid" ) );
            apidatas.put( "workflowid", workflowid );
            apidatas.put( "api_status", true );
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put( "api_status", false );
            apidatas.put( "api_errormsg", "catch exception : " + e.getMessage() );
        }
        return JSONObject.toJSONString( apidatas );
    }

}
